<template>
  <el-dialog
    width="400px"
    :visible.sync="visible"
    title="虚拟机配置"
    :before-close="close"
    :close-on-click-modal="false"
  >
    <el-form class="form" ref="form" :model="vmConfig" label-width="100px">
      <el-form-item label="名称">
        <el-input
          size="mini"
          v-model="vmConfig.name"
          :disabled="!isAdd"
        ></el-input>
      </el-form-item>
      <el-form-item label="CPU">
        <el-input-number
          size="mini"
          v-model="vmConfig.vcpu"
          :min="1"
          :max="16"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="分配内存(MB)">
        <el-input-number
          size="mini"
          v-model="vmConfig.currentMemory"
          :min="256"
          :max="16384"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="镜像路径">
        <!-- <input type="file" name="php_file" id='i-file' onchange="$('#location').val($('#i-file').val());" >
        <input id='location' class="form-control" onclick="$('#i-file').click();"> -->
        <el-input
          size="mini"
          placeholder="请输入内容"
          v-model="vmConfig.disk1Source"
        ></el-input>
      </el-form-item>
      <el-form-item label="网络模式">
        <el-select
          size="mini"
          v-model="vmConfig.network_type"
          placeholder="请选择"
        >
          <el-option label="direct" value="direct"></el-option>
          <el-option label="nat" value="nat"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="直连网卡接口"
        v-show="vmConfig.network_type == 'direct'"
      >
        <el-input
          size="mini"
          placeholder="请输入内容"
          v-model="vmConfig.interface"
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="foot">
      <el-button size="mini" @click="onCancel">取消</el-button>
      <el-button type="primary" size="mini" @click="onSave">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Loading } from "element-ui";
export default {
  name: "MachineEditDialog",
  data() {
    return {
      department: [],
      vmConfig: {
        currentMemory: 512000,
        disk1Source: "/home/str3am/iso/ubuntu-16.04.6-desktop-amd64.iso",
        mac: "00:16:3e:64:01:00",
        memory: 512000,
        name: "test",
        network_type: "default",
        uuid: "test",
        vcpu: 1,
        interface: "",
      },
      newVmConfig: {
        currentMemory: 1024,
        memory: 1024,
        disk1Source: "",
        mac: "00:16:3e:64:01:00",
        name: "test",
        network_type: "default",
        uuid: "test",
        vcpu: 1,
        interface: "",
      },
      visible: false,
      isAdd: false, // 是否为添加
    };
  },
  created() {},
  mounted() {},
  methods: {
    open(val) {
      console.log("val", val);
      if (val != null) {
        this.vmConfig = val;
        this.isAdd = false;
      } else {
        this.isAdd = true;
        this.vmConfig = this.newVmConfig;
      }
      this.visible = true;
    },
    close() {
      this.visible = false;
      this.$emit("close");
    },
    onSave() {
      let obj = {
        name: this.vmConfig.name,
        memory: this.vmConfig.currentMemory,
        disk_1_source: this.vmConfig.disk1Source,
        vcpu: this.vmConfig.vcpu,
        net_model: this.vmConfig.network_type,
        interface: this.vmConfig.interface,
      };

      if (this.isAdd) {
        this.$post("/machine", obj).then((res) => {
          if (res.error == 0) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.close();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        this.$put("/machine", obj).then((res) => {
          if (res.error == 0) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.close();
          } else {
            this.$message.error(res.msg);
          }
        });
      }
      console.log("loadingInstance");
      let loadingInstance = Loading.service({ fullscreen: true });
      this.$nextTick(() => {
        // 以服务的方式调用的 Loading 需要异步关闭
        loadingInstance.close();
      });
    },
    onCancel() {
      this.close();
    },
  },
};
</script>

<style scoped lang="scss">
.form {
  width: 300px;
  margin: 10px;
  .el-input,
  .el-select,
  .el-input-number {
    width: 200px;
    float: right;
  }
}
.foot {
  overflow: hidden;
  .el-button {
    margin: 10px;
    float: right;
  }
}
</style>


